package com.hzit.order.service.impl;

import com.alibaba.fastjson2.JSON;
import com.hzit.order.dto.ItemDTO;
import com.hzit.order.interceptor.LoginInterceptor;
import com.hzit.order.service.ICartService;
import com.ruoyi.common.core.constant.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 类名：
 * 作者：WF
 * 功能：
 */
@Service
public class CartServiceImpl implements ICartService {
	@Autowired
	private StringRedisTemplate redisTemplate;

	//1. 从redis中取出购物车列表
	@Override
	public List<ItemDTO> getCartListFromRedis() {
		//1.1 取得所有数据
		List<Object> values = redisTemplate.opsForHash().values(Constants.CART_PREFIX + LoginInterceptor.local.get());
		//1.2 转换为List<ItemDTO>集合
		return values.stream().map(m -> JSON.parseObject(m + "", ItemDTO.class)).collect(Collectors.toList());
	}
}
